package com.thm.tfast.modules.sys.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thm.tfast.core.constant.MinioBucketConstant;
import com.thm.tfast.core.enums.DeleteFlagEnum;
import com.thm.tfast.core.exception.Asserts;
import com.thm.tfast.core.oss.AbstractOssService;
import com.thm.tfast.core.oss.OssServiceFactory;
import com.thm.tfast.core.sevruity.domain.SysUserDetails;
import com.thm.tfast.core.utils.SecurityUtil;
import com.thm.tfast.modules.sys.mapper.SysFileMapper;
import com.thm.tfast.modules.sys.model.SysFile;
import com.thm.tfast.modules.sys.model.SysUser;
import com.thm.tfast.modules.sys.service.SysFileService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 文件上传记录表 服务实现类
 * </p>
 *
 * @author THM
 * @since 2022-10-27
 */
@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
    @Override
    @Transactional
    public Long upload(MultipartFile file, Integer type) {
        SysFile sysFile = new SysFile();
        //获取桶
        String bucketName = MinioBucketConstant.getBucket(type);
        //原始文件名称
        String realName = file.getOriginalFilename();
        //获取文件后缀
        String extName = FileUtil.extName(realName);
        //生成随机文件名称
        String fileName = IdUtil.fastSimpleUUID() + "." + extName;
        //上传文件
        //获取默认oss服务对象
        AbstractOssService ossService = OssServiceFactory.getInstance(null);
        String path = ossService.upload(bucketName, fileName, file);
        if (StrUtil.isEmpty(path)) Asserts.fail("文件上传失败");
        //保存文件上传记录
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysFile.setBucketName(bucketName);
        sysFile.setFileName(fileName);
        sysFile.setRealName(realName);
        sysFile.setSize(file.getSize());
        sysFile.setSuffix(extName);
        sysFile.setPath(path);
        sysFile.setCreateTime(currentUser.getDate());
        sysFile.setCreateUser(currentUser.getUserId());
        this.save(sysFile);
        return sysFile.getId();
    }

    @Override
    public void download(Long fileId, HttpServletResponse response) {
        SysFile sysFile = this.getById(fileId);
        if (Objects.isNull(sysFile)){
            Asserts.fail("文件不存在");
        }
        AbstractOssService ossService = OssServiceFactory.getInstance(null);
        response.addHeader("Content-Disposition", "attachment;fileName=" + sysFile.getFileName());
        ossService.download(sysFile.getBucketName(),sysFile.getFileName(),response);
    }

    @Override
    public Page<SysFile> page(String keyword, Integer pageNum, Integer pageSize) {
        Page<SysFile> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysFile> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.like(SysFile::getRealName, keyword);
        }
        return this.page(page, wrapper);
    }

    @Override
    @Transactional
    public boolean delete(Long fileId) {
        SysFile sysFile = this.getById(fileId);
        if (Objects.isNull(sysFile)){
            Asserts.fail("文件不存在");
        }
        AbstractOssService ossService = OssServiceFactory.getInstance(null);

        Boolean remove = ossService.remove(sysFile.getBucketName(), sysFile.getFileName());
        if (remove){
            sysFile.setDelFlag(DeleteFlagEnum.DELETED.getCode());
            return this.updateById(sysFile);
        }
        return false;
    }
}
